<?php
namespace app\index\controller;

use app\common\enums\ResultEnum;
use app\common\service\RentService;
use app\common\service\UserService;
use app\common\utils\ResultVOUtil;
use think\Request;
use app\common\model\Agency;
use app\common\service\AccountService;
use app\common\service\LongRentService;
use app\common\service\RentDetailsService;
use app\common\model\Brand;

class Rent extends Base{
    
    private $rentService;
    private $accountService;
    private $userService;
    private $longRentService;
    private $rentDetailsService;
    
    public function __construct(RentService $rentService,AccountService $accountService,UserService $userService,LongRentService $longRentService,RentDetailsService $rentDetailsService)
    {
        parent::initialize();
        $this->rentService = $rentService;
        $this->accountService = $accountService;
        $this->userService = $userService;
        $this->longRentService = $longRentService;
        $this->rentDetailsService = $rentDetailsService;
    }
    
    public function index(Request $request){
        $param = $request->param();
        
        $where = [];
        
        if(!empty($param)){
            if ( !empty($param['search']) ) {
                
                $where[] = ['car_sn|rent_man|contract_number','like','%'.$param['search'].'%'];
            }
            
            if ( !empty($param['user_id']) ) {
                 $where[] = ['user_id','=',$param['user_id']];  
            }

            if ( !empty($param['type']) ) {
                $where[] = ['type','=',$param['type']]; 
            }
            
            if ( !empty($param['brand_id']) ) {
                $where[] = ['brand_id','=',$param['brand_id']];
            }

            $agency_id = session('admin.agency_id');
            if(strpos($agency_id, '1')===false){
                
                if ( !empty($param['agency_id']) && strpos($agency_id, $param['agency_id'])) {
                    $where[] = ['agency_id','=',$param['agency_id']];
                }else{
                    $where[] = ['agency_id','in',$agency_id];
                }
            }else{
                if ( !empty($param['agency_id']) ) {
                    $where[] = ['agency_id','=',$param['agency_id']];
                }
            }
            
            if(!empty($param['dispatch_time'])){
                $where[] = ['dispatch_time','between time',$param['dispatch_time']];
            }
            
            if(!empty($param['back_time'])){
                $where[] = ['back_time','between time',$param['back_time']];
            }
            
            $data = $this->longRentService->index($where,$param['page'],$param['length']);
            if($data){
                return ResultVOUtil::success($data);
            }else{
                return ResultVOUtil::error(ResultEnum::FIND_ERROR);
            }
        }
        
    }
    
    public function list(){
        $data = [];
         
        $data['user'] = $this->userService->list();
        $agency_id = session('admin.agency_id');
        if(strpos($agency_id, '1')===false){
            $data['agency'] = Agency::where('id','in',$agency_id)->select();
        }else{
            $data['agency'] = Agency::all();
        }
        $data['brand'] = Brand::all();
        return ResultVOUtil::success($data);
    }
    
    public function save(Request $request){
        $param = $request->param();
        if(!empty($param)){
            $data = $this->longRentService->save($param);
        	if($data){
        	    return ResultVOUtil::success($data);
        	}else{
        	    return ResultVOUtil::error(ResultEnum::WRITE_ERROR);
        	}
        }       
    }
    
    public function details($num){
        $data = $this->rentService->details($num);
        if($data){
            return ResultVOUtil::success($data);
        }else{
            return ResultVOUtil::error(ResultEnum::FIND_ERROR);
        }

    }

    public function dispatch(Request $request){
    	$param = $request->param();
    	if(!empty($param)){
    	    $data = $this->longRentService->dispatch($param);
     	    if($data){
    	        $res = $this->longRentService->account($param);
    	        if($res){
    	            return ResultVOUtil::success('');
    	        }else{
    	            return ResultVOUtil::error(ResultEnum::WRITE_ERROR);
    	        }
    	      
     	     }else{
     	       return ResultVOUtil::error(ResultEnum::WRITE_ERROR);
     	     } 
    	}
    }
    
    public function account(Request $request){
        $param = $request->param();
        $where = [];
        
        if(!empty($param)){
            if ( !empty($param['search']) ) {
                
                $where[] = ['trade_no','like','%'.$param['search'].'%'];
            }
            
            if ( !empty($param['charge_status']) ) {
                $where[] = ['charge_status','=',$param['charge_status']];
            }
            
            if ( !empty($param['user_id']) ) {
                $where[] = ['user_id','=',$param['user_id']];
            }
            
            $data = $this->rentService->account($where,$param['page'],$param['length']);
            
            if($data){
                return ResultVOUtil::success($data);
            }else{
                return ResultVOUtil::error(ResultEnum::FIND_ERROR);
            }
        }
       
    }
    
    public function rentList(Request $request){
        $param = $request->param();
        $where = [];
        if(!empty($param)){
            if ( !empty($param['search']) ) {
                
                $where[] = ['rent_man|contract_number','like','%'.$param['search'].'%'];
            }
            
            if ( !empty($param['user_id']) ) {
                $where[] = ['user_id','=',$param['user_id']];
            }
  
            if ( !empty($param['type']) ) {
                $where[] = ['type','=',$param['type']];     
            }
            
            if ( !empty($param['brand_id']) ) {
                $where[] = ['brand_id','=',$param['brand_id']];    
            }
            
            $agency_id = session('admin.agency_id');
            if(strpos($agency_id, '1')===false){
                
                if ( !empty($param['agency_id']) && strpos($agency_id, $param['agency_id'])) {
                    $where[] = ['agency_id','=',$param['agency_id']];
                }else{
                    $where[] = ['agency_id','in',$agency_id];
                }
            }else{
                if ( !empty($param['agency_id']) ) {
                    $where[] = ['agency_id','=',$param['agency_id']];
                }
            }
            
            if(!empty($param['belongs_month'])){
                $where[] = ['belongs_month','=',strtotime($param['belongs_month'])];
            }
            
            if(!empty($param['actual_time'])){
                $where[] = ['actual_time','=',strtotime($param['actual_time'])];
            }
            
          
            $data = $this->rentDetailsService->index($where,$param['page'],$param['length']);
            
            if($data){
                return ResultVOUtil::success($data);
            }else{
                return ResultVOUtil::error(ResultEnum::FIND_ERROR);
            }
        }
    }

    public function edit(Request $request){
        $param = $request->param();
        if(!empty($param)){
          
            $data = $this->longRentService->edit($param);
            if($data){
                return ResultVOUtil::success($data);
            }else{
                return ResultVOUtil::error(ResultEnum::WRITE_ERROR);
            }
        }
    }
    
    public function detailsEdit(Request $request){
        $param = $request->param();
        if(!empty($param)){
            $data = $this->rentDetailsService->edit($param);
            if($data){
                return ResultVOUtil::success($data);
            }else{
                return ResultVOUtil::error(ResultEnum::WRITE_ERROR);
            }
        }
    }
    
   

    public function upload(Request $request)
    {
        $param = $request->param();
        if(!empty($param)){
            $data = $this->longRentService->uploadFile($param);
            if($data){
                return ResultVOUtil::success($data);
            }else{
                return ResultVOUtil::error(ResultEnum::WRITE_ERROR);
            }
        }
    }
    
    public function detailsUpload(Request $request){
        $param = $request->param();
        if(!empty($param)){
            $data = $this->rentDetailsService->uploadFile($param);
            if($data){
                return ResultVOUtil::success($data);
            }else{
                return ResultVOUtil::error(ResultEnum::WRITE_ERROR);
            }
        }
    }
    
    public function delete($id = 0){
        $data = $this->longRentService->delete($id);
        if($data){
            return ResultVOUtil::success('');
        }else{
            return ResultVOUtil::error(ResultEnum::DEL_ERROR);
        }
    }
    
    public function detailsDelete($id = 0){
        $data = $this->rentDetailsService->delete($id);
        if($data){
            return ResultVOUtil::success('');
        }else{
            return ResultVOUtil::error(ResultEnum::DEL_ERROR);
        }
    }
    
    public function rentLog(Request $request){
        $param = $request->param();
        if(!empty($param)){
            $data = $this->longRentService->rentLog($param);
            if($data){
                return ResultVOUtil::success($data);
            }else{
                return ResultVOUtil::error(ResultEnum::FIND_ERROR);
            }
        }
        
    }
    
    public function detailsLog(Request $request){
        $param = $request->param();
        if(!empty($param)){
            $data = $this->rentDetailsService->rentLog($param);
            if($data){
                return ResultVOUtil::success($data);
            }else{
                return ResultVOUtil::error(ResultEnum::FIND_ERROR);
            }
        }
    }

    
    public function changeStatus(Request $request){
        $param = $request->param();
        if(!empty($param)){
            $data = $this->longRentService->changeStatus($param);
            if($data){
                return ResultVOUtil::success($data);
            }else{
                return ResultVOUtil::error(ResultEnum::UPDATE_ERROR);
            }
        }
    }
    
    public function renew(Request $request){
        $param = $request->param();
        if(!empty($param)){
            $data = $this->longRentService->renew($param);
            if($data){
                return ResultVOUtil::success('');
            }else{
                return ResultVOUtil::error(ResultEnum::UPDATE_ERROR);
            }
        }
    }
    
}